package com.hotacorp.opencloud.authserver.feign;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.hotacorp.opencloud.common.commdata.Constants;

import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * feign拦截器，如果上级访问有Tenant-Id头，那么就带着这个头访问下级
 * @author lwg
 *
 */
@Service
public class AddTenantIdRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
    	ServletRequestAttributes dddAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
    	if (dddAttributes != null) {
    		HttpServletRequest request = dddAttributes.getRequest();
    		String dddString = request.getHeader(Constants.TENANT_ID_HEAD);
    		if (!StringUtils.isBlank(dddString)) {
    			template.header(Constants.TENANT_ID_HEAD, dddString);
    		}
    	}
    }

}